import java.util.Timer;
import java.util.TimerTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: WJW
 * Date: 2024-08-11
 * Time: 10:13
 */
//时间定时器
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        Timer timer = new Timer();
        timer.schedule(new TimerTask(){
            @Override
            public void run() {
                System.out.println("hello timer 3000");
            }
        },3000);

        timer.schedule(new TimerTask(){
            @Override
            public void run() {
                System.out.println("hello timer 2000");
            }
        },2000);

        timer.schedule(new TimerTask(){
            @Override
            public void run() {
                System.out.println("hello timer 1000");
            }
        },1000);

        System.out.println("hello main");

        Thread.sleep(5000);

        //需要主动添加cancel主动结束，否则Timer不知道其他地方还要继续添加任务
        timer.cancel();
    }
}
